package com.woniuxy.project115.kaer.tenant.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.project115.kaer.commons.util.JwtUtil;
import com.woniuxy.project115.kaer.commons.util.Result;
import com.woniuxy.project115.kaer.tenant.dto.TenantStoreDto;
import com.woniuxy.project115.kaer.tenant.entity.TenantStore;
import com.woniuxy.project115.kaer.tenant.form.UpdateStoreInfoForm;
import com.woniuxy.project115.kaer.tenant.form.UserSearchForm;
import com.woniuxy.project115.kaer.tenant.param.UpdateStoreInfoParam;
import com.woniuxy.project115.kaer.tenant.param.UserSearchParam;
import com.woniuxy.project115.kaer.tenant.service.TenantStoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuH
 * @since 2025年02月20日
 */
@RestController
@RequestMapping("/tenantStore")
@Api(tags = "商家店铺信息接口")
public class TenantStoreController {
    @Resource
    private TenantStoreService tenantStoreService;

    @PostMapping("/getStoreInfo")
    @ApiOperation("获取商家店铺信息")
    public Result<TenantStore> getStoreInfo(@RequestHeader String token) throws Exception {
        Integer tenantInfoId = JwtUtil.getTenantInfoId(token);
        return Result.succeed("success", tenantStoreService.getStoreInfo(tenantInfoId));
    }

    @PostMapping("/updateStoreInfo")
    @ApiOperation("更新商家店铺信息")
    public Result<TenantStore> updateStoreInfo(@RequestBody UpdateStoreInfoForm form, @RequestHeader String token) throws Exception {
        Integer tenantInfoId = JwtUtil.getTenantInfoId(token);
        tenantStoreService.updateStoreInfo(BeanUtil.toBean(form, UpdateStoreInfoParam.class), tenantInfoId);
        return Result.succeed("success");
    }

    @PostMapping("/userSearch")
    @ApiOperation("用户搜索")
    public Result<Page<TenantStoreDto>> userSearch(@RequestBody UserSearchForm form, @RequestHeader String token) throws Exception {
        Integer userId = JwtUtil.getUserId(token);
        return Result.succeed("success", tenantStoreService.userSearch(BeanUtil.toBean(form, UserSearchParam.class)));
    }

    @PostMapping("/startAndEndBusiness")
    @ApiOperation("商家开始营业、结束营业")
    public Result<TenantStore> startAndEndBusiness(@RequestHeader String token) throws Exception {
        Integer storeId = JwtUtil.getStoreId(token);
        tenantStoreService.startAndEndBusiness(storeId);
        return Result.succeed("success");
    }
}

